//
//  UIButton+ROCameraPhotoes.m
//  Runone
//
//  Created by runone02 on 16/12/29.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "UIButton+ROCameraPhotoes.h"

@implementation UIButton (ROCameraPhotoes)

- (void)adjustImagePositon:(ROBtnImagePosition)imagePosition spacing:(CGFloat)spacing {
    [self layoutIfNeeded];
    [self.titleLabel sizeToFit];
    
    //图片
    CGFloat imageWidth  = self.imageView.image.size.width;
    CGFloat imageHeight = self.imageView.image.size.height;
    
    //文字label
    CGFloat labelWidth  = [self.titleLabel.text sizeWithFont:self.titleLabel.font].width;
    CGFloat labelHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font].height;
    
    //x
    CGFloat imageOffsetX = (imageWidth + labelWidth) / 2 - imageWidth / 2;
    CGFloat imageOffsetY = (imageHeight + spacing) / 2;
    CGFloat labelOffsetX = (imageWidth + labelWidth) / 2 - (imageWidth + labelWidth) / 2;
    
    // y
    CGFloat labelOffsetY = labelHeight / 2 + spacing / 2;
    
    CGFloat tempWidth     = MAX(labelWidth, imageWidth);
    CGFloat changedWidth  = labelWidth + imageWidth - tempWidth;
    CGFloat tempHeight    = MAX(labelHeight, imageHeight);
    CGFloat changedHeight = labelHeight + imageHeight + spacing - tempHeight;

    switch (imagePosition) {
        case ROCameraPhotoesBtnLeft:
            self.imageEdgeInsets   = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2);
            self.titleEdgeInsets   = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2);
            self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
            break;
            
        case ROCameraPhotoesBtnRight:
            self.imageEdgeInsets   = UIEdgeInsetsMake(0, labelWidth + spacing/2, 0, -(labelWidth + spacing/2));
            self.titleEdgeInsets   = UIEdgeInsetsMake(0, -(imageWidth + spacing/2), 0, imageWidth + spacing/2);
            self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
            break;
            
        case ROCameraPhotoesBtnTop:
            self.imageEdgeInsets   = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
            self.titleEdgeInsets   = UIEdgeInsetsMake(labelOffsetY, -labelOffsetX, -labelOffsetY, labelOffsetX);
            self.contentEdgeInsets = UIEdgeInsetsMake(imageOffsetY, -changedWidth/2, changedHeight-imageOffsetY, -changedWidth/2);
            break;
            
        case ROCameraPhotoesBtnBottom:
            self.imageEdgeInsets   = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);
            self.titleEdgeInsets   = UIEdgeInsetsMake(-labelOffsetY, -labelOffsetX, labelOffsetY, labelOffsetX);
            self.contentEdgeInsets = UIEdgeInsetsMake(changedHeight-imageOffsetY, -changedWidth/2, imageOffsetY, -changedWidth/2);
            break;
            
        default:
            break;
    }

}


@end
